package com.shop.mms_client.utils;

import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.RequestCallback;
import java.util.ArrayList;
import java.util.List;

public class PermissionManager {
    private static final String[] REQUIRED_PERMISSIONS = {
        android.Manifest.permission.RECEIVE_SMS,
        android.Manifest.permission.READ_SMS,
        android.Manifest.permission.INTERNET,
        android.Manifest.permission.SEND_SMS
    };

    public interface PermissionCallback {
        void onGranted();
        void onDenied(List<String> deniedPermissions);
    }

    public static void checkAndRequestPermissions(AppCompatActivity activity, PermissionCallback callback) {
        List<String> permissionsToRequest = new ArrayList<>();
        
        for (String permission : REQUIRED_PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(activity, permission) 
                != PackageManager.PERMISSION_GRANTED) {
                permissionsToRequest.add(permission);
            }
        }

        if (permissionsToRequest.isEmpty()) {
            callback.onGranted();
            return;
        }

        PermissionX.init(activity)
            .permissions(permissionsToRequest)
            .request(new RequestCallback() {
                @Override
                public void onResult(boolean allGranted, List<String> grantedList, 
                                   List<String> deniedList) {
                    if (allGranted) {
                        callback.onGranted();
                    } else {
                        callback.onDenied(deniedList);
                    }
                }
            });
    }
} 